using LiteDB;
using Newtonsoft.Json;

namespace LiteDb
{
    public class ConfigSettingRepository
    {
        private readonly LiteDatabase _db;
        private readonly ILiteCollection<ConfigSetting> _collection;

        public ConfigSettingRepository(LiteDatabase db)
        {
            _db = db;
            _collection = _db.GetCollection<ConfigSetting>(ConfigSetting.CollectionName);
        }

        /// <summary>
        /// 保存配置项
        /// </summary>
        public object SaveConfig(object param)
        {
            try
            {
                if (param == null)
                {
                    return ApiResponse.Fail("参数不能为空");
                }

                var config = JsonConvert.DeserializeObject<ConfigSetting>(param.ToString() ?? "");
                if (config == null)
                {
                    return ApiResponse.Fail("参数格式错误");
                }

                // 检查是否存在相同的Key
                var existingConfig = _collection.FindOne(x => x.Key == config.Key);
                if (existingConfig != null)
                {
                    // 更新现有配置
                    _collection.Update(config);
                    return ApiResponse.Success(config, "配置更新成功");
                }
                else
                {
                    // 插入新配置
                    _collection.Insert(config);
                    return ApiResponse.Success(config, "配置保存成功");
                }
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail($"保存配置失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取配置项
        /// </summary>
        public object GetConfig(object param)
        {
            try
            {
                if (param == null)
                {
                    return ApiResponse.Fail("参数不能为空");
                }

                string key = param.ToString() ?? "";
                var config = _collection.FindOne(x => x.Key == key);
                
                if (config == null)
                {
                    return ApiResponse.Fail("未找到指定的配置项");
                }

                return ApiResponse.Success(config.Value, "获取配置成功");
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail($"获取配置失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取所有配置项
        /// </summary>
        public object GetAllConfigs(object param)
        {
            try
            {
                var configs = _collection.FindAll().ToList();
                return ApiResponse.Success(configs, "获取所有配置成功");
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail($"获取所有配置失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 删除配置项
        /// </summary>
        public object DeleteConfig(object param)
        {
            try
            {
                if (param == null)
                {
                    return ApiResponse.Fail("参数不能为空");
                }

                string key = param.ToString() ?? "";
                bool deleted = _collection.Delete(key);
                
                if (!deleted)
                {
                    return ApiResponse.Fail("未找到指定的配置项或删除失败");
                }

                return ApiResponse.Success(true, "配置删除成功");
            }
            catch (Exception ex)
            {
                return ApiResponse.Fail($"删除配置失败: {ex.Message}");
            }
        }
    }
} 